package it.intecs.pisa.smarcos;

import it.intecs.pisa.smarcos.services.HumanInteractionService;
import it.intecs.pisa.smarcos.services.HumanInteractionService.ResultReceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;


public class main extends Activity  {

	private static final String TAG = main.class.getSimpleName();
	private Intent srvIntent;
	private IntentFilter stopFilter;
	private StopService stopSrvReceiver = null;

	@Override	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.d(TAG, "OnCreate");

		stopFilter = new IntentFilter("CLOSE SERVICE");
		stopFilter.addCategory(Intent.CATEGORY_DEFAULT);      
		stopSrvReceiver = new StopService();
		registerReceiver(stopSrvReceiver, stopFilter);
		
		srvIntent = new Intent(this, HumanInteractionService.class);
		srvIntent.addCategory(Intent.CATEGORY_DEFAULT);
		startService(srvIntent);
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		
		Log.d(TAG, "OnDestroy");
		unregisterReceiver(stopSrvReceiver);
	}
	
	private class StopService extends BroadcastReceiver {

		//public static final String INTENT_SHOW="it.intecs.pisa.smarcos.services.StopService.SHOW";
		private final String TAG = ResultReceiver.class.getSimpleName();

		@Override
		public void onReceive(Context arg0, Intent intent) {
			Log.d(TAG, "OnReceive: Stop Human Interction Service");
			stopService(srvIntent);
			finish();
		}			
	}
}


	